<template>
  <el-dialog :title="'编辑会员'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" 
      label-width="100px">
      <el-form-item label="上传头像" >
        <el-button v-if="dataForm.headUrl == '' || dataForm.headUrl == null" type="primary"
          @click="uploadHandle()">点击上传</el-button>
        <img class="Avatars" v-if="dataForm.headUrl" :src="'https://qxbh5.oss-cn-guangzhou.aliyuncs.com' + dataForm.headUrl" alt=""
          @click="uploadHandle()" />
        <p class="ae">建议尺寸：180*180，，图片小于5M</p>
      </el-form-item>
      <el-form-item label="昵称：" prop="nickName">
        <el-input v-model="dataForm.nickName" placeholder="请输入" style="width: 220px"></el-input>
      </el-form-item>
      <el-form-item label="用户权限：">
        <div class="rolewrap">
          <div class="list" v-for="(item, index) in categoryList" :key="index" style="margin-top:15px;">
            <div class="menu2">
              一级：<el-checkbox v-model="item.check" @change="setALL(item)">{{ item.categoryName }}</el-checkbox>
            </div>
            <div class="menu3" v-if="item.childs.length">
               二级：<el-checkbox v-model="i.check" v-for="(i, x) in item.childs" :key="x">{{ i.categoryName }}</el-checkbox>
              </div>
          </div>
        </div>
      </el-form-item>
      <el-form-item label="超级用户：">
        <div>
          <el-radio v-model="dataForm.superman" :label="1">是</el-radio>
          <el-radio v-model="dataForm.superman" :label="0">否</el-radio>
        </div>
      </el-form-item>
    </el-form>
    <upload v-if="uploadVisible" ref="upload" :uploadList='uploadList'></upload>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import Upload from './oss-upload'
export default {
  data () {
    return {
      uploadVisible: false,
      isdisabled: false,
      visible: false,
      categoryList: [],
      dataForm: {
        categoryIds: [],
        id: 0,
        type: '',
        nickName: '',
        headUrl: '',
        superman: 0
      },
      dataRule: {
        nickName: [
          { required: true, message: '请输入昵称', trigger: 'blur' }
        ]
        // updateTime: [
        //   { required: true, message: '修改时间不能为空', trigger: 'blur' }
        // ]
      }
    }
  },
  components: {
    Upload
  },
  methods: {
    setALL (e) {
      let isTrue = e.check
      e.childs.forEach((item, index) => {
        item.check = isTrue
      })
    },
    init (infoCount) {
      // console.log("111")
      this.dataForm.id = Number(infoCount.id) || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.dataForm.headUrl = infoCount.headUrl
          this.dataForm.nickName = infoCount.nickName
          this.dataForm.superman = infoCount.superman
          this.categoryList = infoCount.categoryList
        }
      })
    },
    uploadHandle () {
      this.uploadVisible = true
      this.$nextTick(() => {
        this.$refs.upload.init()
      })
    },
    uploadList (e) {
      this.dataForm.headUrl = e.trim()
    },
    // 表单提交
    dataFormSubmit () {
      this.dataForm.categoryIds = []
      this.categoryList.forEach((item) => {
        if (item.check) {
          this.dataForm.categoryIds.push(item.id)
        }
        item.childs.forEach((i) => {
          if (i.check) {
            this.dataForm.categoryIds.push(i.id)
          }
        })
      })
      this.$refs['dataForm'].validate((valid) => {
        let headUrl = {}
        if (this.dataForm.headUrl) {
          headUrl.headUrl = this.dataForm.headUrl
        }
        this.isdisabled = true
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/admin/user/update`),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id,
              nickName: this.dataForm.nickName,
              categoryIds: this.dataForm.categoryIds,
              ...headUrl,
              superman: this.dataForm.superman
            })
          }).then(({ data }) => {
            if (data && data.code === '200') {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
              this.isdisabled = false
            } else {
              this.$message.error(data.msg)
              this.isdisabled = false
            }
          })
        }
      })
    }
  }
}
</script>
<style >
.ae {
  color: red;
  font-size: 12px;
  line-height: 15px;
  margin: 0;
  margin-top: 10px;
}

.Avatars {
  width: 50px;
  height: 50px;
  border: 1px solid #ccc;
}

.v-modal {
  z-index: 1999 !important;
}
.list {
  margin-left: 10px;
  margin-top: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #dcdfe6;
  .menu {
    flex: 1;
  }
  .menu2 {
    flex: 2;
    height: 25px;
    line-height: 25px;
  }
  .menu3 {
    flex: 1;
  }
}
.rolewrap{
  max-height: 340px;
  overflow-y: scroll;
}
.list:first-of-type{
  margin-top: 0px;
}
</style>